package com.sxp.servlet;

import com.sxp.bean.User;
import com.sxp.dao.BookDao;
import com.sxp.dao.impl.BookDaoImpl;
import com.sxp.bean.Book;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author 宋晓盼
 * @since 2019/6/8 22:19
 */
@WebServlet("/book/search")
public class SearchBookServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String bookName = req.getParameter("bookname");
        HttpSession session = req.getSession();

        if (bookName != null) {
            BookDao bookDao = new BookDaoImpl();
            List<Book> books = bookDao.listByName(bookName);
            session.setAttribute("searchBooks", books);
            req.getRequestDispatcher("/search.jsp").forward(req, resp);
        } else {
            session.setAttribute("msg", "书名不能为空！");
            resp.sendRedirect("/obs/message.jsp");
        }

        System.out.println(bookName);
    }
}
